<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('编辑')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="535px" @close='close' top="10vh">
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" :inline="true" label-width="130px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('服务器IP')" prop="host">
          <el-input v-emoji v-model="record.host" ref="host" style="width:290px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('加密方式')" prop="encryption" style="width: 230px;">
          <el-select v-model="record.encryption" @change="changeSelect" ref="encryption" style="width:100px;" size='small'>
            <el-option v-for="(item, key) in encryptionArray" :key="key" :label="item" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('服务器端口')" prop="port" label-width="77px" style="width: 180px;">
          <el-input v-emoji v-model="record.port" ref="port" style="width:100px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('邮件发送者')" prop="email">
          <el-input v-emoji v-model="record.email" style="width:290px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('验证方式')" prop="method">
          <el-radio-group v-model="record.method">
            <el-radio label="login" class="login">{{$t('登录')}}</el-radio>
            <el-radio label="none" class="none">{{$t('无')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('用户名')" prop="username">
          <el-input v-emoji v-model="record.username" ref="name" :disabled='record.method === "none"' style="width:290px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('密码')" prop="password">
          <el-input v-emoji v-model="record.password" ref="password" :disabled='record.method === "none"' style="width:290px;" size='small' @focus="handleFocus" @blur="handleBlur" class="inputPsd"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { setSmtp } from '@/api/system/alarm/config'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'ConfigDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      let reg = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!reg.test(value)) {
        callback(new Error(this.$t('邮箱格式不正确')))
      } else {
        callback()
      }
    }
    var validateHost = (rule, value, callback) => {
      let reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!reg.test(value)) {
        callback(new Error(this.$t('IP格式不正确')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        host: '',
        encryption: 'plain',
        port: '',
        email: '',
        method: 'none',
        username: '',
        password: ''
      },
      recordCopy: {
        host: '',
        encryption: 'plain',
        port: '',
        email: '',
        method: 'none',
        username: '',
        password: ''
      },
      encryptionNew: {},
      encryptionArray: {
        'plain': this.$t('明文'),
        'ssl': 'SSL',
        'tls': 'TLS'
      },
      encryptionPort: {
        'plain': 25,
        'ssl': 465,
        'tls': 587
      },
      rules: {
        host: [{ required: false, validator: validateHost, trigger: 'blur' }],
        encryption: [{ required: false, validator: validateName, trigger: 'change' }],
        port: [{ required: false, validator: validateName, trigger: 'change' }],
        email: [{ required: false, validator: validateEmail, trigger: 'blur' }],
        method: [{ required: false, validator: validateName, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['host', 'password', 'name', 'port']
        const selectData = ['encryption']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
        this.record.email = data['sender-email']
        this.record.username = data.user
        this.record.method = data['auth-method']
        this.encryptionNew[data.encryption] = data.port
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setSmtp(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('编辑成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.encryptionNew = {}
      this.$refs.form.resetFields()
    },
    changeSelect (data) {
      this.record.port = this.encryptionNew[data] ? this.encryptionNew[data] : this.encryptionPort[data]
    },
    // 隐藏密码
    handleFocus () {
      this.$refs.password = 'password'
    },
    handleBlur () {
      this.$refs.password = 'text'
    }
  }
}
</script>
<style lang="less" scoped>
.inputPsd {
  -webkit-text-security: disc;
}
</style>
